package com.bonjour.work.service;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.bonjour.common.core.BaseService;
import com.bonjour.common.core.RpcReply;
import com.bonjour.common.helper.BeanHelper;
import com.bonjour.common.helper.DateHelper;
import com.bonjour.common.helper.EncryptHelper;
import com.bonjour.common.helper.SnowIdHelper;
import com.bonjour.common.helper.StringHelper;
import com.bonjour.common.view.parser.RequestContext;
import com.bonjour.work.dao.UserDao;
import com.bonjour.work.dto.request.user.LoginReq;
import com.bonjour.work.dto.request.user.RegistryReq;
import com.bonjour.work.dto.request.user.UpdatePwdReq;
import com.bonjour.work.dto.request.user.UpdateUserReq;
import com.bonjour.work.entity.UserEntity;
import com.bonjour.work.kern.CodeMSG;
import com.bonjour.work.utils.enums.UserStatus;

@Service
public class UserService extends BaseService {

    private final UserDao userDao;

    @Inject
    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public RpcReply<Void> registry(RegistryReq registryReq) {
        if (StringUtils.isEmpty(registryReq.email) && StringUtils.isEmpty(registryReq.userName)) {
            return RpcReply.onFail(CodeMSG.PARAM_ERROR);
        }

        int count = userDao.userCount(registryReq.email, registryReq.userName);
        if (count != 0) {
            return RpcReply.onFail(CodeMSG.USER_ALREADY_EXIST);
        }

        UserEntity user = new UserEntity();
//        user.setId(SnowIdHelper.nextId());
        BeanHelper.copy(registryReq, user);
        user.password = EncryptHelper.md5(registryReq.password);
        user.userStatus = 1;
        userDao.insert(user);
        return RpcReply.onOk();

    }


    public RpcReply<String> login(LoginReq req) {
        if (!StringUtils.hasLength(req.email) && !StringUtils.hasLength(req.userName)) {
            return RpcReply.onFail(CodeMSG.NO_SUCH_USER);
        }

        UserEntity user = userDao.selectUser(req.email, req.userName);
        if (user == null) {
            return RpcReply.onFail(CodeMSG.NO_SUCH_USER);
        }

        String pwd = EncryptHelper.md5(req.password);
        if (!user.password.equals(pwd)) {
            return RpcReply.onFail(CodeMSG.PWD_ERROR);
        }
        if (user.userStatus == UserStatus.FROZEN.getType()) {
            return RpcReply.onFail(CodeMSG.FORBIDDEN_ERROR);
        }
        long expire = DateHelper.second() + DateHelper.DAY_TIME / DateHelper.SECOND_TIME;
        String token = EncryptHelper.signatureJWT(StringHelper.EMPTY,
                String.valueOf(user.getId().toString()), "", expire);
        return RpcReply.onOk(token);
    }

    public RpcReply<UserEntity> getUserMsg() {
        Long userId = RequestContext.get().getSession().uid;
        return RpcReply.onOk(userDao.getUserById(userId));
    }

//    public RpcReply inviteUserToProject(InviteUserToProjectReq req) {
//
//        List<UserProjectRelations> list = Lists.newArrayList();
//        req.userIds.forEach(userId -> {
//            UserProjectRelations relations = new UserProjectRelations();
//            relations.isDeleted = IsDeletedEnum.ENABLE.getType();
//            relations.projectId = req.projectId;
//            relations.type = req.permissionType;
//            relations.setId(SnowIdHelper.nextId());
//            relations.userId = userId;
//            list.add(relations);
//        });
//        userProjectRelationsDao.batchInsert(list);
//
//        return RpcReply.onOk();
//    }


    public RpcReply<Void> updatePwd(UpdatePwdReq pwdReq) {

        String pwd = EncryptHelper.md5(pwdReq.pwd);
        Long userId = RequestContext.get().getSession().uid;
        UserEntity user = userDao.getUserById(userId);
        if (user == null) {
            return RpcReply.onFail(CodeMSG.NO_SUCH_USER);
        }
        if (!user.password.equals(pwd)) {
            return RpcReply.onFail(CodeMSG.OLD_PWD_ERROR);
        }
        UserEntity u = new UserEntity();
        u.password = EncryptHelper.md5(pwdReq.newPwd);
        u.setId(userId);
        userDao.update(u);

        return RpcReply.onOk();
    }


    public RpcReply<Void> updateUserStatus(UpdateUserReq user) {
        UserEntity userEntity = new UserEntity();
        userEntity.userStatus = user.userStatus;
        userDao.update(userEntity);
        return RpcReply.onOk();
    }

}
